﻿// TestCaseModel
// (c)2007 Mark Bulmahn. mBulmahn@web.de
// License: GPL 2.0
// Project: TestCaseGenerator. http://www.codeplex.com/TestCaseGenerator

using System;
using System.Collections.Generic;
using System.Text;

namespace TestCaseModel
{
    /// <summary>
    /// This list holds many equivalent classes
    /// </summary>
    [Serializable]
    public class EquivClasses : List<EquivClass>
    {
        public bool HasId(string id)
        {
            foreach (EquivClass c in this)
                if (c.Id == id)
                    return true;
            return false;
        }

        public bool RemoveById(string id)
        {
            for (int i = 0; i < this.Count; i++)
                if (this[i].Id == id)
                    return Remove(this[i]);
            return false;
        }

        public EquivClass GetById(string id)
        {
            foreach (EquivClass c in this)
            {
                if (c.Id == id)
                    return c;
            }
            throw new Exception("No such Class with ID='" + id + "'");
        }

        public string GetExampleData(string id)
        {
            foreach (EquivClass c in this)
            {
                if (c.Id == id)
                    return c.ExampleData;
            }
            throw new Exception("No such Class with ID='" + id + "'");
        }

        public List<EquivClass> SortedClasses
        {
            get
            {
                SortedDictionary<int, EquivClass> sorted = new SortedDictionary<int, EquivClass>();
                foreach (EquivClass c in this)
                {
                    sorted.Add(c.OrderId, c);
                }
                return (new List<EquivClass>(sorted.Values));
            }
        }

        public int GetNewOrderId()
        {
            int max = 0;
            foreach (EquivClass c in this)
                if (c.OrderId > max)
                    max = c.OrderId;
            return max + 1; 
        }

        public void MoveDown(EquivClass currentClass)
        {
            foreach (EquivClass c in this)
            {
                if (c.OrderId == currentClass.OrderId + 1)
                {
                    c.OrderId--;
                    currentClass.OrderId++;
                    break;
                }
            }
        }

        public void MoveUp(EquivClass currentClass)
        {
            foreach (EquivClass c in this)
            {
                if (c.OrderId == currentClass.OrderId - 1)
                {
                    c.OrderId++;
                    currentClass.OrderId--;
                    break;
                }
            }
        }
    }
}
